package com.project.erp.service.contract.trade.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.project.erp.common.utils.Page;
import com.project.erp.dao.contract.trade.TradeCusContractDao;
import com.project.erp.dao.contract.trade.TradeCusContractDtlDao;
import com.project.erp.dao.fobcost.FobCostDao;
import com.project.erp.model.contract.trade.TradeCusContract;
import com.project.erp.model.contract.trade.TradeCusContractDtl;
import com.project.erp.service.contract.trade.TradeCusContractService;

@Service
@Transactional
public class TradeCusContractServiceImpl implements TradeCusContractService {

	@Autowired
	private TradeCusContractDao tradeCusContractDao;
	@Autowired
	private TradeCusContractDtlDao tradeCusContractDtlDao;
	@Autowired
	private FobCostDao fobCostDao;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Page<TradeCusContract> getTradeCusContractPageBySearch(
			Page<TradeCusContract> page, TradeCusContract tradeCusContract) {
		return tradeCusContractDao.getTradeCusContractPageBySearch(page, tradeCusContract);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveTradeCusContract(TradeCusContract tradeCusContract) {
		if(tradeCusContract.getCreateTime() == null){
			tradeCusContract.setCreateTime(new Date());
		}
		tradeCusContractDao.save(tradeCusContract);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public TradeCusContract getTradeCusContractById(Long tradeCusContractId) {
		return tradeCusContractDao.get(tradeCusContractId);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void editTradeCusContract(TradeCusContract tradeCusContract,List<TradeCusContractDtl> tradeCusContractDtlList,
			String[] tradeCusContractDtlIds) {
		List<Long> delTradeCusContractDtlIds = new ArrayList<Long>();
		boolean delflag;//是否删除记录
		
		//检查是否有删除的记录
		if(tradeCusContractDtlIds != null){//针对细表记录为空情况
			for(TradeCusContractDtl tradeCusContractDtl:tradeCusContractDtlList){
				delflag = true;
				String tradeCusContractDtlId1 = "";
				String tradeCusContractDtlId2 = "";
				for(int i=0;i<tradeCusContractDtlIds.length;i++){
					
					if(tradeCusContractDtlIds[i] != null){
						tradeCusContractDtlId1 = tradeCusContractDtlIds[i].trim();
						tradeCusContractDtlId2 = String.valueOf(tradeCusContractDtl.getId());
						if(tradeCusContractDtlId1.equals(tradeCusContractDtlId2)){
							delflag = false;
						}
					}
				}
				if(delflag){
					//增加要删除的记录id到列表
					delTradeCusContractDtlIds.add(tradeCusContractDtl.getId());
				}
			}
		}else{
			//删除全部细表记录
			if((tradeCusContractDtlList != null) && (tradeCusContractDtlList.size() > 0)){
				for(TradeCusContractDtl tradeCusContractDtl:tradeCusContractDtlList){
					delTradeCusContractDtlIds.add(tradeCusContractDtl.getId());
				}
			}
		}
		
		//先删除要删除的细表记录
		if((delTradeCusContractDtlIds != null) && (delTradeCusContractDtlIds.size() > 0)){
			tradeCusContractDtlDao.deleteByIdsList(delTradeCusContractDtlIds);
		}
		
		tradeCusContract.setUpdateTime(new Date());
		tradeCusContractDao.save(tradeCusContract);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void delTradeCusContract(String[] tradeCusContractIds) {
		if(null == tradeCusContractIds || tradeCusContractIds.length == 0){
			return;
		}
		List<Long> ids = new ArrayList<Long>();
		for (String tradeCusContractId : tradeCusContractIds) {
			ids.add(Long.valueOf(tradeCusContractId));
		}
		//删除主表信息
		fobCostDao.delFobCostByfobCostIds(ids);
	}

}
